﻿using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Models.Entitys;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Repository;
using RndMaterialDisposalService.Services.Interface.SmallBatchTrial;
using Masuit.Tools;
using Masuit.Tools.Core.AspNetCore;

namespace RndMaterialDisposalService.Services.SmallBatchTrialService
{
    /// <summary>
    /// 
    /// </summary>
    [NAutowired]
    public class WorkshopProcessingService : IWorkshopProcessingService
    {

        private readonly AssetsManagementContext dbContext;
        private readonly WorkflowCommonDataContext commonDataContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_dbContext"></param>
        /// <param name="_commonDataContext"></param>
        public WorkshopProcessingService(AssetsManagementContext _dbContext, WorkflowCommonDataContext _commonDataContext)
        {
            dbContext = _dbContext;
            commonDataContext = _commonDataContext;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputReq"></param>
        /// <returns></returns>
        public async Task<(List<WorkshopProcessinReportDto> list, int count)> GetReportData(WorkshopProcessinReportReq inputReq)
        {
            var list = dbContext.WorkshopProcessinReport.Where(p => 1 == 1);
            if (!string.IsNullOrEmpty(inputReq.MainFlowId))
            {
                list = list.Where(p => p.ParentFlowId == inputReq.MainFlowId);
            }
            if (!string.IsNullOrEmpty(inputReq.ProcessFolio))
            {
                list = list.Where(p => p.Folio.Contains(inputReq.ProcessFolio));
            }
            if (!string.IsNullOrEmpty(inputReq.ProcessingType))
            {
                list = list.Where(p => p.ProcessingType == inputReq.ProcessingType);
            }
            if (!string.IsNullOrEmpty(inputReq.Applicant))
            {
                list = list.Where(p => p.Applicant == inputReq.Applicant);
            }
            if (inputReq.StartDate != null && inputReq.EndDate != null)
            {
                list = list.Where(p => p.ApplicationDate > inputReq.StartDate && p.ApplicationDate < inputReq.EndDate);
            }
            if (!string.IsNullOrEmpty(inputReq.Activity))
            {
                list = list.Where(p => p.ActivityName == inputReq.Activity);
            }


            var count = await list.CountAsync();
            var data = await list.OrderByDescending(p => p.ApplicationDate)
                .Skip((inputReq.PageIndex - 1) * inputReq.PageSize)
                .Take(inputReq.PageSize).ToListAsync();

            return (data, count);
        }

        /// <summary>
        /// 保存报表数据
        /// </summary>
        /// <param name="reportModel"></param>
        /// <returns></returns>
        public async Task<int> SaveReportData(WorkshopProcessinReportDto reportModel)
        {
            dbContext.WorkshopProcessinReport.AddOrUpdate(p => p.ProcInstId, reportModel);
            return await dbContext.SaveChangesAsync();
        }
    }
}
